package com.adcc.loadsheet.billing.validator;

import com.adcc.aoc.devLib.common.log.Log;
import com.adcc.loadsheet.billing.entity.chargerule.BillingRuleEntity;
import com.adcc.loadsheet.billing.vo.chargerule.BillingLadderEntityVO;
import com.adcc.loadsheet.billing.vo.chargerule.BillingRuleEntityVO;
import com.google.common.base.Strings;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by zhaoml on 2020/4/7.
 * 计费规则VO校验
 */
@Component
public class BillingRuleEntityVOValidator extends ValidatorAdapter<BillingRuleEntityVO> {

    @Override
    public void customValidate(BillingRuleEntityVO vo, Errors errors) {
        try {
            //规则简称不能为空
            if (Strings.isNullOrEmpty(vo.getRuleAbbrev())) {
                errors.reject("规则简称不能为空！");
                return;
            }
            //规则简称长度不能大于20
            if (vo.getRuleAbbrev().length() > 40) {
                errors.reject("规则简称长度不能大于40！");
                return;
            }
            //规则全称长度不能大于20
            if (!Strings.isNullOrEmpty(vo.getRuleFullname()) && vo.getRuleFullname().length() > 200) {
                errors.reject("规则全称长度不能大于200！");
                return;
            }
            //备注长度不能大于500
            if (vo.getRemark() != null && vo.getRemark().length() > 500) {
                errors.reject("备注长度不能大于500！");
                return;
            }

            //验证计费阶梯的开始数和结束数是否连续或重复，如有不符则进行提示“阶梯计数不连续，请核对！”
            List<BillingLadderEntityVO> ladderEntityVOList = vo.getBillingLadderEntityVOList();
            if(ladderEntityVOList != null && ladderEntityVOList.size() > 0){
                BillingLadderEntityVO currentLadderEntityVO = null;   //当前循环的阶梯
                Integer lastLadderEndnumber = 0;
                for(int i = 0;i < ladderEntityVOList.size();i ++){
                    currentLadderEntityVO = ladderEntityVOList.get(i);

                    //计费单价超限提醒“阶梯单价超出范围，请核对！”
                    if(currentLadderEntityVO.getUnitPrice().compareTo(BigDecimal.valueOf(1000)) != -1){
                        errors.reject("阶梯单价超出范围，请核对！");
                        return;
                    }

                    if(i == 0 && !currentLadderEntityVO.getLadderStartnumber().equals(1)){
                        errors.reject("阶梯价初始值需从1开始！");
                        return;
                    }

                    //对比当前的开始计数和上次的结束计数 || 当前的开始计数和结束计数做对比
                    if((!currentLadderEntityVO.getLadderStartnumber().equals(lastLadderEndnumber + 1))
                            || currentLadderEntityVO.getLadderStartnumber() >= currentLadderEntityVO.getLadderEndnumber()){
                        errors.reject("阶梯计数不连续，请核对！");
                        return;
                    }
                    lastLadderEndnumber = currentLadderEntityVO.getLadderEndnumber();
                }
            }

            //规则简称是否重复
            BillingRuleEntity entity = billingRuleService.findByRuleAbbrev(vo.getRuleAbbrev());
            if (entity != null) {
                if (entity.getBillingRuleId().equals(vo.getBillingRuleId())){
                    //说明没有修改
                    return;
                }
                //新建、修改时不能重复
                errors.reject("规则简称不能重复！");
                return;
            }
        } catch (Exception e) {
            Log.error(BillingRuleEntityVOValidator.class, "customValidate() validate验证出现错误", e);
            errors.reject("数据输入有误！");
        }
    }
}